package com.tengju.user.infrastructure.repository.archive;

import com.tengju.user.domain.model.archive.letax.LetaxAccountId;
import com.tengju.user.domain.model.archive.letax.LetaxSnapshot;
import com.tengju.user.domain.model.archive.letax.LetaxSnapshotId;
import com.tengju.user.domain.model.archive.letax.LetaxSnapshotRepository;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.LetaxRegisteredReturnDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.generate.LetaxRegisteredReturnDOMapper;
import com.tengju.user.infrastructure.repository.convert.LetaxSnapshotConvert;
import org.springframework.stereotype.Repository;

@Repository
public class LetaxSnapshotRepositoryImpl implements LetaxSnapshotRepository {

    private final LetaxRegisteredReturnDOMapper letaxRegisteredReturnDOMapper;

    public LetaxSnapshotRepositoryImpl(LetaxRegisteredReturnDOMapper letaxRegisteredReturnDOMapper) {
        this.letaxRegisteredReturnDOMapper = letaxRegisteredReturnDOMapper;
    }

    @Override
    public LetaxSnapshot find(LetaxAccountId letaxAccountId) {
        LetaxRegisteredReturnDO letaxRegisteredReturnDO = letaxRegisteredReturnDOMapper.findByRegisteredId(letaxAccountId.getValue());
        return LetaxSnapshotConvert.convert(letaxRegisteredReturnDO);
    }

    @Override
    public int update(LetaxSnapshotId letaxSnapshotId, LetaxSnapshot letaxSnapshot) {
        LetaxRegisteredReturnDO letaxRegisteredReturnDO = LetaxSnapshotConvert.convert(letaxSnapshotId, letaxSnapshot);
        return letaxRegisteredReturnDOMapper.updateByPrimaryKeySelective(letaxRegisteredReturnDO);
    }

    @Override
    public LetaxSnapshotId save(LetaxSnapshot letaxSnapshot) {
        LetaxRegisteredReturnDO letaxRegisteredReturnDO = LetaxSnapshotConvert.convert(letaxSnapshot);
        letaxRegisteredReturnDOMapper.insertSelective(letaxRegisteredReturnDO);
        return new LetaxSnapshotId(letaxRegisteredReturnDO.getRegisteredId());
    }
}
